<template>
  <div class="app-container">
    <el-form :model="formData" label-width="120px" label-position="left">
      <el-form-item label="角色名称">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="角色权限">
        <div v-for="(authorityCategory,i) in authorityCategoryList">
          <el-checkbox :indeterminate="isIndeterminate[i]" v-model="checkAll[i]" @change="handleCheckAll(i)">
            {{authorityCategory.authorityCategoryName}}
          </el-checkbox>
          <el-checkbox-group v-model="checkedAuthorities[i]" @change="handleAuthorityChange(i)">
            <el-checkbox v-for="authority in authorityCategory.authorityList" :label="authority.authorityCode">
              {{authority.authorityName}}
            </el-checkbox>
          </el-checkbox-group>
          <div style="margin-top: 15px;"></div>
        </div>
      </el-form-item>
    </el-form>
    <div class="submit-box">
      <el-button type="primary" :loading="loading" style="width: 200px;" @click.native.prevent="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
  import { create } from '@/api/role'
  import { getAll } from '@/api/authority'
  import { errorMsg, successMsg } from '@/utils/my'

  export default {
    data() {
      return {
        loading: false,
        formData: {
          name: '',
          authorities: ''
        },
        authorityCategoryList: [],
        checkAll: [],
        isIndeterminate: [],
        authorityList: [],
        checkedAuthorities: [],
        allAuthorities: []
      }
    },

    mounted() {
      this.getAllAuthorities()
    },

    methods: {

      handleCheckAll(i) {
        if (this.checkAll[i]) {
          this.checkedAuthorities[i] = this.allAuthorities[i]
        } else {
          this.checkedAuthorities[i] = []
        }
      },

      getAllAuthorities() {
        const that = this
        getAll({}).then(res => {
          that.authorityCategoryList = res.data
          for (let i = 0; i < that.authorityCategoryList.length; i++) {
            that.allAuthorities[i] = []
            that.authorityList[i] = that.authorityCategoryList[i].authorityList
            that.checkedAuthorities[i] = []
            that.isIndeterminate[i] = false
            that.checkAll[i] = false
            for (let j = 0; j < that.authorityCategoryList[i].authorityList.length; j++) {
              that.allAuthorities[i].push(that.authorityCategoryList[i].authorityList[j].authorityCode)
            }
          }
        })
      },

      doSubmit() {
        let that = this,
            d = that.formData,
            authorities = []

        for (let i = 0; i < this.checkedAuthorities.length; i++) {
          if (this.checkedAuthorities[i].length > 0) {
            authorities.push(...this.checkedAuthorities[i])
          }
        }
        d.authorities = authorities.join(',')

        if (d.name.length <= 0) {
          return errorMsg('请输入角色名称')
        }
        if (authorities.length <= 0) {
          return errorMsg('请至少选择一个权限')
        }

        that.loading = true
        create(that.formData).then(res => {
          that.loading = false
          if (res.code != 20000) {
            return errorMsg(res.message)
          }
          successMsg(res.message)
          setTimeout(function() {
            that.$router.push({path: '/admin/role'})
          }, 1500)
        })
      },

      handleAuthorityChange(i) {
        let count = this.authorityList[i].length,
            checkedCount = this.checkedAuthorities[i].length
        if (checkedCount > 0 && checkedCount < count) {
          this.isIndeterminate[i] = true
          this.checkAll[i] = false
        } else if (checkedCount > 0 && checkedCount == count) {
          this.isIndeterminate[i] = false
          this.checkAll[i] = true
        } else {
          this.isIndeterminate[i] = false
          this.checkAll[i] = false
        }
        this.$forceUpdate()
      }

    }
  }
</script>

<style>
  .submit-box {
    height: 50px;
    line-height: 50px;
    text-align: center;
    padding-right: 20px;
  }
</style>
